﻿using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;

namespace CommonUtils
{
    [SwaggerSchema("数据分页")]
    public class Page<T>
    {
        //post的话设置默认参数需封装成[int?],判断变为==null||<1 get.int默认会给0
        //[SwaggerParameter("页码")][FromForm] int pageNum = 1,
        //[SwaggerParameter("页容")][FromForm] int pageSize = 10
        //写成默认参数 swagger报错 postman不报错????
        [SwaggerSchema("页码")]
        public int Num { get; private set; }

        [SwaggerSchema("页容")]
        public int Size { get; private set; }

        [SwaggerSchema("查询跳过")]
        [JsonIgnore]
        public int Skip
        {
            get
            {
                return (Num - 1) * Size;
            }
        }

        [SwaggerSchema("数据总量")]
        public int Amount { get; set; }

        [SwaggerSchema("数据")]
        public T[] Records { get; set; } = { };

        public Page(int? num, int? size)
        {
            if (num == null || num < 1)
                num = 1;
            Num = num.Value;
            if (size == null || size < 0)
                size = 10;
            Size = size.Value;
        }
    }
}
